package xps.and.uudaijia.userclient.util;

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.alipay.sdk.app.PayTask;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import xps.and.uudaijia.userclient.App;

/**
 * Created by XPS on 2017/7/6.
 */

public class APay {

    private static final String APPID = "2017070407643405";

    private static final String RSA_PRIVATE = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBALHr4bRoNuKXGzOaqwRT0K5l1oAqEN72qvWABpxciKb/28sQNe0pW/1PNeqFaPJ1O8s2PixM+BE8GA7gT1FVM4Q84hN3jUmhYJya4xkY9O3a7u6W30IHuV1uTrlemd+yeE97W1NoVywB3ohWlrJVq32dKJeZyqtAS3r4hpgS38lHAgMBAAECgYEAsOlX8vfQ4zvW+1fmhpYEoPR39dZeEiV5VkTnzeAgrNUqKl7DED+YzWZOuRPQh41UWDpmVcxuuUxkq54UoE20uzXw0LpHyBtUmALta9PZ43GvPSo3xJxpd+uf/C1VFlxpzzNShqSaR4cgrBhxB3bE6b84LWHJW8lBH/GWwcpsHkECQQDlsl7A8RAGb31/Q57Tx4twQmospuMiGn4DfNZsRTepVNq0pSzwF5FcZROm22/kpSM6aO/7r0RcXIwfpwAN11AnAkEAxkuznezxMeQuArLssrxxceFHBsxahxvTKvl0fO9CZVbnuSnKFz4ZVaqYrcdN/fc69BBS7GchECAQ2nEqm0pR4QJBAIaHodd/WNNsGSSghjWjeZeWTnk4hW9D9Oo2A6AGKkOS30VOOLOmGgzzZj8j5rUTwz5Cl8vhKzitO6SOOwngQaECQBrs/pfMK7gtlr3RE+j1Q6ZGA8H9j7FW6uaj9thcGLTwBwkSjpl3WjNSteaq8IEyx/OKH+hv3IJ3CDLROQrbHaECQQDWddCWTLFjs0rux/obYnDHIAiBa28jHpjc60cdb7X8mlhPXXU4SwIHxLVAc32kmLxtQTXwqaIkitbmZl1LJ9tR";

    private static final APay ourInstance = new APay();

    public static APay getInstance() {
        return ourInstance;
    }

    private APay() {
    }

    public void payV2(final Activity ac) {
        /**
         * note : 订单信息最好全由服务器生成并返回
         *         本地不保留privateKey 和 appid
         *         请求支付时 仅提交subject和价格
         * */
        //构建参数集合 sign时会用到
        Map<String, String> params = OrderInfoUtil.buildOrderParamMap(APPID);
        //构建参数字符串   把每一个key的value都encode
        String orderParam = OrderInfoUtil.buildOrderParam(params);

        //签名参数字符串
        String sign = OrderInfoUtil.getSign(params, RSA_PRIVATE);//--> sign=ce6e8wchwc9q3cq87q3ch

        //参数字符串 拼接上 签名 生成的订单信息
        final String orderInfo = orderParam + "&" + sign;

        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
//                PayTask alipay = new PayTask(ac);
//                final Map<String, String> result = alipay.payV2(orderInfo, true);
//
//                Set<String> set = result.keySet();
//                Collection<String> collections = result.values();
//
//                Iterator<String> vs = collections.iterator();
//                Iterator<String> ks = set.iterator();
//
//                while (ks.hasNext()) {
//                    Log.e("TAG ks", ks.next());
//                }
//
//                while (vs.hasNext()) {
//                    Log.e("TAG vs", vs.next());
//                }

                //payResult(result);
            }
        };

        Thread payThread = new Thread(payRunnable);
        payThread.start();
    }

    private void payResult(Map<String, String> result) {
        PayResult payResult = new PayResult(result);
        /**
         对于支付结果，请商户依赖服务端的异步通知结果。同步通知结果，仅作为支付结束的通知。
         */
        String resultInfo = payResult.getResult();// 同步返回需要验证的信息
        String resultStatus = payResult.getResultStatus();
        // 判断resultStatus 为9000则代表支付成功
        if (TextUtils.equals(resultStatus, "9000")) {
            // 该笔订单是否真实支付成功，需要依赖服务端的异步通知。
            Toast.makeText(App.getInstance(), "支付成功", Toast.LENGTH_SHORT).show();
        } else {
            // 该笔订单真实的支付结果，需要依赖服务端的异步通知。
            Toast.makeText(App.getInstance(), "支付失败", Toast.LENGTH_SHORT).show();
        }
    }

}
